package day05;

public class 反转链表 {
    /*public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        // 利用头插法
        // 定义一个虚拟节点
        ListNode ret = new ListNode(0);
        while(head != null){
            ListNode headNext = head.next;
            head.next = ret.next;
            ret.next = head;
            head = headNext;
        }
        return ret.next;
    }*/

    //2025.6.30 复习

    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        // 不定义虚拟节点
        ListNode cur = head.next;
        head.next = null;

        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
}
